import { ajax } from '../../js/ajax.js';

let tbUser;
let tbUserInfo;
let userOtherInfo;

//#region 获取用户信息的部分
let imgUser = document.getElementById('imgUser');
let spName = document.getElementById('spName');
let spNickname = document.getElementById('spNickname');
let spSex = document.getElementById('spSex');
let spRegDay = document.getElementById('spRegDay');

function queryUserInfo() {
  ajax.post('/user/auth/getUserInfo', {}, (data) => {
    if (data.success) {
      tbUser = data.tbUser;
      tbUserInfo = data.tbUserInfo;
      userOtherInfo = data.userOtherInfo;
      showUserInfo();
    } else {
      alert('请先登录！');
    }
  });
}

let sexInfo = {
  m: '男',
  f: '女',
  n: '保密',
};

function showUserInfo() {
  spName.innerHTML = tbUser.username;
  spNickname.innerHTML = tbUser.nickname;
  spSex.innerHTML = sexInfo[tbUserInfo.sex];
  if (tbUserInfo.img) {
    imgUser.setAttribute('src', tbUserInfo.img);
  }
  spRegDay.innerHTML = userOtherInfo.regDays;
}

queryUserInfo();
//#endregion

//#region 修改用户信息的部分
let btnShowModify = document.getElementById('btnShowModify');
let divInfo = document.getElementById('divInfo');
let txt_img = document.getElementById('txt_img');
let txt_info = document.getElementById('txt_info');
let txt_nickname = document.getElementById('txt_nickname');
let txt_qq = document.getElementById('txt_qq');
let txt_wechat = document.getElementById('txt_wechat');
let sexm = document.getElementById('sexm');
let sexf = document.getElementById('sexf');
let sexn = document.getElementById('sexn');
let btnSave = document.getElementById('btnSave');

divInfo.style.display = 'none';

btnShowModify.addEventListener('click', () => {
  txt_nickname.value = tbUser.nickname;
  txt_img.value = tbUserInfo.img;
  txt_info.value = tbUserInfo.info;
  txt_qq.value = tbUserInfo.qq;
  txt_wechat.value = tbUserInfo.wechat;
  //性别比较麻烦，要判定用户的选择项
  if (tbUserInfo.sex == 'm') {
    sexm.checked = true;
  } else if (tbUserInfo.sex == 'f') {
    sexf.checked = true;
  } else if (tbUserInfo.sex == 'n') {
    sexn.checked = true;
  }
  divInfo.style.display = 'block';
});

btnSave.addEventListener('click', () => {
  let sex = 'n';
  if (sexm.checked) {
    sex = 'm';
  } else if (sexf.checked) {
    sex = 'f';
  }
  let info = {
    nickname: txt_nickname.value,
    img: txt_img.value,
    info: txt_info.value,
    qq: txt_qq.value,
    wechat: txt_wechat.value,
    sex: sex,
  };
  ajax.post('/user/auth/updateUserInfo', info, (data) => {
    alert(data.message);
    //更新用户信息
    queryUserInfo();
  });
});

//#endregion

//#region 绑定邮箱的部分

let btnShowEmail = document.getElementById('btnShowEmail');
let divModifyEmail = document.getElementById('divModifyEmail');
let txt_email = document.getElementById('txt_email');
let btnSendEmail = document.getElementById('btnSendEmail');
let txt_email_code = document.getElementById('txt_email_code');
let btnSaveEmail = document.getElementById('btnSaveEmail');

divModifyEmail.style.display = 'none';
btnShowEmail.addEventListener('click', () => {
  txt_email.value = tbUserInfo.email;
  divModifyEmail.style.display = 'block';
});

//发验证码
btnSendEmail.addEventListener('click', () => {
  ajax.post('/tool/sendEmailCode', { email: txt_email.value }, (data) => {
    alert(data.message);
  });
});

btnSaveEmail.addEventListener('click', () => {
  ajax.post(
    '/user/auth/updateUserEmail',
    {
      email: txt_email.value,
      code: txt_email_code.value,
    },
    (data) => {
      alert(data.message);
    }
  );
});
//#endregion

//#region 安全退出

let btnLogout = document.getElementById('btnLogout');
btnLogout.addEventListener('click', () => {
  ajax.post('/user/auth/logout', {}, () => {
    alert('安全退出完成');
    location.reload();
  });
});

//#endregion
